using Godot;
using System;

public partial class AreaMonster : Area2D
{
	private bool haveEntry = false;
	private Card _targeCard;
	private int grade;
	private int atkTemp = 0;

	private int defTemp = 0;
	private Godot.Collections.Dictionary data;
	[Export]
	public Sprite2D parentNode { get; set; }
	[Export]
	public Sprite2D parentNode2 { get; set; }
	[Export]
	public Sprite2D parentNode3 { get; set; }
	[Signal]
	public delegate void useMonstSignalEventHandler(Card p, int grade);
	public override void _Ready()
	{
		using FileAccess file = FileAccess.Open("res://data.json", FileAccess.ModeFlags.Read);
		string jsonText = file.GetAsText();
		Json json = new Json();
		Error error = json.Parse(jsonText);
		data = json.Data.AsGodotDictionary();
		MouseEntered += OnBodyEntered;
		MouseExited += OnBodyExited;
	}
	public void OnBodyEntered()
	{
		haveEntry = true;
	}
	public void OnBodyExited()
	{
		haveEntry = false;

	}
	public override void _Input(InputEvent @event)
	{
		if (@event is InputEventMouseButton mouseEvent &&
		mouseEvent.ButtonIndex == MouseButton.Left)
		{
			if (mouseEvent.Pressed)
			{
				if (haveEntry)
				{
					if (grade == 2)
					{
						
						var jsonData = data["card"].AsGodotArray();
						var tmp = jsonData[31].AsGodotDictionary();
						_targeCard.Texture = GD.Load<Texture2D>("res://photo/card/low/" + tmp["name"].AsString() + ".png");
						_targeCard.name = tmp["name"].AsString();
						_targeCard.changeAD(tmp["atk"].AsInt32(), tmp["def"].AsInt32());
						_targeCard.grade = tmp["grade"].AsInt32();
						_targeCard.attribute = tmp["attribute"].AsString();
						EmitSignal(SignalName.useMonstSignal, _targeCard, grade);
					}
					else if (grade == 4)
					{
						
						var jsonData = data["card"].AsGodotArray();
						var tmp = jsonData[34].AsGodotDictionary();
						_targeCard.Texture = GD.Load<Texture2D>("res://photo/card/low/" + tmp["name"].AsString() + ".png");
						_targeCard.name = tmp["name"].AsString();
						_targeCard.changeAD(tmp["atk"].AsInt32(), tmp["def"].AsInt32());
						_targeCard.grade = tmp["grade"].AsInt32();
						_targeCard.attribute = tmp["attribute"].AsString();
						EmitSignal(SignalName.useMonstSignal, _targeCard, grade);
					}
					else if (grade == 6)
					{
						
						var jsonData = data["card"].AsGodotArray();
						var tmp = jsonData[37].AsGodotDictionary();
						_targeCard.Texture = GD.Load<Texture2D>("res://photo/card/low/" + tmp["name"].AsString() + ".png");
						_targeCard.name = tmp["name"].AsString();
						_targeCard.changeAD(tmp["atk"].AsInt32(), tmp["def"].AsInt32());
						_targeCard.grade = tmp["grade"].AsInt32();
						_targeCard.attribute = tmp["attribute"].AsString();
						EmitSignal(SignalName.useMonstSignal, _targeCard, grade);
					}
					else if (grade == 8)
					{
						
						var jsonData = data["card"].AsGodotArray();
						var tmp = jsonData[40].AsGodotDictionary();
						_targeCard.Texture = GD.Load<Texture2D>("res://photo/card/low/" + tmp["name"].AsString() + ".png");
						_targeCard.name = tmp["name"].AsString();
						_targeCard.changeAD(tmp["atk"].AsInt32(), tmp["def"].AsInt32());
						_targeCard.grade = tmp["grade"].AsInt32();
						_targeCard.attribute = tmp["attribute"].AsString();
						EmitSignal(SignalName.useMonstSignal, _targeCard, grade);
					}
					else if (grade == 10)
					{
						
						var jsonData = data["card"].AsGodotArray();
						var tmp = jsonData[43].AsGodotDictionary();
						_targeCard.Texture = GD.Load<Texture2D>("res://photo/card/low/" + tmp["name"].AsString() + ".png");
						_targeCard.name = tmp["name"].AsString();
						_targeCard.changeAD(atkTemp + _targeCard.atk, defTemp + _targeCard.def);
						_targeCard.capacityDef = true;
						_targeCard.grade = tmp["grade"].AsInt32();
						_targeCard.attribute = tmp["attribute"].AsString();
						EmitSignal(SignalName.useMonstSignal, _targeCard, grade);
					}
					else
					{

					}
					parentNode.Visible = false;
					parentNode3.Visible = false;
					parentNode2.Visible = false;

				}
			}
		}
	}
	public void showCardSignalHandler(int grade, Card node, int atkTemp, int defTemp)
	{
		this.atkTemp = atkTemp;
		this.defTemp = defTemp;
		this.grade = grade;
		_targeCard = node;
		if (grade == 2)
		{
			parentNode.Texture = GD.Load<Texture2D>("res://photo/card/high/坤坤.png");
			parentNode.Visible = true;
		}else if(grade == 4){
			parentNode.Texture = GD.Load<Texture2D>("res://photo/card/high/小约翰可汗.png");
			parentNode.Visible = true;
		}else if(grade == 6){
			parentNode.Texture = GD.Load<Texture2D>("res://photo/card/high/诸葛琴魔.png");
			parentNode.Visible = true;
		}else if(grade == 8){
			parentNode.Texture = GD.Load<Texture2D>("res://photo/card/high/卢本伟.png");
			parentNode.Visible = true;
		}else if(grade == 10){
			parentNode.Texture = GD.Load<Texture2D>("res://photo/card/high/耄耋.png");
			parentNode.Visible = true;
		}else{
			
		}
	}
}
